//
//  WCEditIncludeDirectoriesSheetController.m
//  WabbitCode
//
//  Created by William Towe on 4/3/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCEditIncludeDirectoriesSheetController.h"
#import "WCBuildConfiguration.h"
#import "WCBuildTargetGetInfoWindowController.h"
#import "WCIconAndTextFieldCell.h"
#import "WCIncludeDirectory.h"
#import "WCAddIncludeDirectoryViewController.h"


@implementation WCEditIncludeDirectoriesSheetController
- (id)init {
	if (!(self = [super initWithWindowNibName:@"WCEditIncludeDirectoriesSheet"]))
		return nil;
	
	_includeDirectories = [[NSMutableArray alloc] init];
	
	return self;
}

- (void)dealloc {
	[_includeDirectories release];
	[super dealloc];
}

+ (void)presentEditIncludeDirectoriesSheetForBuildConfiguration:(WCBuildConfiguration *)config forGetInfoWindowController:(WCBuildTargetGetInfoWindowController *)getInfoWindowController; {
	WCEditIncludeDirectoriesSheetController *controller = [[[self class] alloc] init];
	
	[controller setBuildConfiguration:config];
	[controller setGetInfoWindowController:getInfoWindowController];
	
	for (WCIncludeDirectory *include in [config includeDirectories]) {
		[[controller includeDirectories] addObject:[[include copy] autorelease]];
	}
	
	[NSApp beginSheet:[controller window] modalForWindow:[getInfoWindowController window] modalDelegate:controller didEndSelector:@selector(_sheetDidEnd:code:info:) contextInfo:NULL];
}

@synthesize buildConfiguration=_buildConfiguration,getInfoWindowController=_getInfoWindowController,includeDirectories=_includeDirectories;

- (IBAction)addIncludeDirectoryAction:(id)sender; {
	NSOpenPanel *panel = [NSOpenPanel openPanel];
	
	[panel setCanChooseFiles:NO];
	[panel setCanChooseDirectories:YES];
	[panel setAllowsMultipleSelection:YES];
	
	WCAddIncludeDirectoryViewController *controller = [WCAddIncludeDirectoryViewController addIncludeDirectoryViewController];
	
	[panel setAccessoryView:[controller view]];
	
	if ([panel runModalForDirectory:nil file:nil] != NSOKButton)
		return;
	
	NSArray *currentDirectoryPaths = [[self includeDirectories] valueForKey:@"directoryPath"];
	for (NSString *directoryPath in [panel filenames]) {
		if ([currentDirectoryPaths containsObject:directoryPath])
			continue;
		
		[[_tableView dataSource] addObject:[WCIncludeDirectory includeDirectoryWithDirectoryPath:directoryPath forBuildConfiguration:[self buildConfiguration]]];
		
		if (![controller recursivelyAddSubdirectories])
			continue;
		
		BOOL isDirectory = NO;
		for (NSString *relativePath in [[NSFileManager defaultManager] enumeratorAtPath:directoryPath]) {
			NSString *subDirectoryPath = [directoryPath stringByAppendingPathComponent:relativePath];
			if ([relativePath hasPrefix:@"."] || ![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath isDirectory:&isDirectory] || !isDirectory || [[NSWorkspace sharedWorkspace] isFilePackageAtPath:subDirectoryPath])
				continue;
			
			[[_tableView dataSource] addObject:[WCIncludeDirectory includeDirectoryWithDirectoryPath:subDirectoryPath forBuildConfiguration:[self buildConfiguration]]];
		}
	}
}

- (void)_sheetDidEnd:(NSWindow *)window code:(NSInteger)code info:(void *)info {
	[self autorelease];
	if (code != NSOKButton)
		return;
	
	[[self buildConfiguration] setIncludeDirectories:[self includeDirectories]];
}
@end
